package com.example.hp.yuejian5.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.example.hp.yuejian5.R;
import com.example.hp.yuejian5.fragment.ClubFragment;
import com.example.hp.yuejian5.fragment.CourseFragment;
import com.example.hp.yuejian5.fragment.IndexFragment;
import com.example.hp.yuejian5.fragment.MasterFragment;
import com.example.hp.yuejian5.fragment.ShowFragment;
import com.example.hp.yuejian5.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

public class FragmentActivity extends AppCompatActivity {


    private LinearLayout layout1,layout2,layout3,layout4,layout5,layout6;
    private ViewPager vp;
    private List<Fragment> fragmentList;
    private FragmentPagerAdapter fpadapter;
    private Toolbar toolbar;
    private TextView toolbartitle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_fragment);
        initView();
        toolbartitle.setText("首页");
        toolbartitle.setTextColor(getResources().getColor(android.R.color.white));//设置标题的字体颜色
        setSupportActionBar(toolbar);
        initEvents();
        initFragment();
    }

    private void initView()
    {
        toolbar=(Toolbar) findViewById(R.id.toolbar);
        toolbartitle=findViewById(R.id.toolbartitle);
        layout1=findViewById(R.id.layout1);
        layout2=findViewById(R.id.layout2);
        layout3=findViewById(R.id.layout3);
        layout4=findViewById(R.id.layout4);
        layout5=findViewById(R.id.layout5);
        layout6=findViewById(R.id.layout6);
        vp=findViewById(R.id.vp);

    }
    private void initEvents()
    {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
        layout6.setOnClickListener(onClickListener);
    }

    private void selectTab(int i)
    {

        switch (i)
        {
            case 0:

                toolbartitle.setText("首页");

                break;
            case 1:

                toolbartitle.setText("俱乐部");
                break;

            case 2:

                toolbartitle.setText("课程");
                break;

            case 3:
                toolbartitle.setText("大师");
                break;

            case 4:
                toolbartitle.setText("晒图");
                break;
            case 5:
                toolbartitle.setText("我的");
                break;

        }
        vp.setCurrentItem(i);
    }

    private void  initFragment()
    {
        fragmentList=new ArrayList<Fragment>();

        fragmentList.add(new IndexFragment());
        fragmentList.add(new ClubFragment());
        fragmentList.add(new CourseFragment());
        fragmentList.add(new MasterFragment());
        fragmentList.add(new ShowFragment());
        fragmentList.add(new UserFragment());
        fpadapter=new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        vp.setAdapter(fpadapter);
        vp.setOffscreenPageLimit(2);//viewpager预加载数据
        vp.setCurrentItem(0);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);

                selectTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId())
            {
                case R.id.layout1:selectTab(0);

                    break;
                case R.id.layout2:selectTab(1);

                    break;
                case R.id.layout3:selectTab(2);

                    break;
                case R.id.layout4:selectTab(3);
                    break;
                case R.id.layout5:selectTab(4);
                    break;
                case R.id.layout6:selectTab(5);

                    break;
            }
        }
    };
}

